package com.odm.communitymanage.ui.notifications

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ResourceUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import kotlinx.android.synthetic.main.fragment_notices.*
import org.w3c.dom.Text

/**
 * @description: 通告公告页面
 * @author: ODM
 * @date: 2020/4/15
 */
class NoticesFragment : BaseMVVMFragment() {

    //假数据 通知列表
    var noticeList : MutableList<NoticeData> = mutableListOf()

    var adapter : NoticesAdapter ?= null

    init {
        noticeList.add(NoticeData(0 , "通知标题~通知标题" ,"本次通知的内容主要为我们要争取文明社区,大家要共同努力","本次通知的内容主要为我们要争取...","2020-03-24  09:38"))
        noticeList.add(NoticeData(1 , "调研标题~调研标题" ,"本次调研的内容：社区争取文明社区,想调研下大家的意向","本次调研的内容：社区争取文明社...","2020-03-24  09:53"))
        noticeList.add(NoticeData(2 , "投票标题~投票标题" ,"投票的内容主要为我们要争取文明社区,大家需要投票哪一样举措应该实行","投票的内容主要为我们要争取文明...","2020-03-24  10：20"))
        noticeList.add(NoticeData(3 , "公告标题~公告标题" ,"本次公告的内容主要为我们社区已经被评为文明五星社区,近日将会举办线上表彰大会，请各单位做好充足的准备","本次公告的内容主要为我们社区已...","2020-03-24  09:39"))
        noticeList.add(NoticeData(0 , "通知标题~通知标题" ,"本次通知的内容主要为我们要争取文明社区,大家要共同努力","本次通知的内容主要为我们要争取...","2020-03-24  09:41"))
        noticeList.add(NoticeData(0 , "通知标题~通知标题" ,"本次通知的内容主要为我们要争取文明社区,大家要共同努力","本次通知的内容主要为我们要争取...","2020-03-24  09:38"))
        noticeList.add(NoticeData(1 , "调研标题~调研标题" ,"本次调研的内容：社区争取文明社区,想调研下大家的意向","本次调研的内容：社区争取文明社...","2020-03-24  09:53"))
        noticeList.add(NoticeData(2 , "投票标题~投票标题" ,"投票的内容主要为我们要争取文明社区,大家需要投票哪一样举措应该实行","投票的内容主要为我们要争取文明...","2020-03-24  10：20"))
    }

    override fun initViewModelObserve() {

    }

    override fun initViews() {
        rvNotices.layoutManager = LinearLayoutManager(requireContext())
        adapter = NoticesAdapter(noticeList)
        rvNotices.adapter = adapter

        //点击发送一个通知类对象进入详情通知页
        adapter?.setOnItemClickListener { adapter, view, position ->
            LiveEventBus.get(Constants.EVENT_NOTICE_DETAIL)
                .post(adapter.data[position])
            //从容器Activity中找出父Fragment，调用启动 通知详情页
            _mActivity.findFragment( NotificationsFragment::class.java).startDetailFragment()
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_notices
    }

    override fun shouldPopUpCloseDialog(): Boolean {
        return true
    }

    data class NoticeData(
        //0 通知  1 调研  2 公告  3 投票
        val type : Int,
        val title : String,
        val content : String ,
        val summary : String,
        val start_time : String
    )


    class NoticesAdapter (data : MutableList<NoticeData>)
        : BaseQuickAdapter<NoticeData, BaseViewHolder>(R.layout.item_notice , data) {

        override fun convert(holder: BaseViewHolder, item: NoticeData) {
            //根据种类，分配类型
            when(item.type){
                0 -> {
                    holder.setText(R.id.tvType_Notice , "通知")
                    holder.setTextColor(R.id.tvType_Notice  , ColorUtils.string2Int("#FF8A8A"))
                    holder.getView<TextView>(R.id.tvType_Notice).background = ResourceUtils.getDrawable(R.drawable.shape_frame_red_no_radius)
                }
                1 -> {
                    holder.setText(R.id.tvType_Notice , "调研")
                    holder.setTextColor(R.id.tvType_Notice  , ColorUtils.string2Int("#61A4E4"))
                    holder.getView<TextView>(R.id.tvType_Notice).background = ResourceUtils.getDrawable(R.drawable.shape_frame_blue_no_radius)
                }
                2 -> {
                    holder.setText(R.id.tvType_Notice , "公告")
                    holder.setTextColor(R.id.tvType_Notice  , ColorUtils.string2Int("#FFA958"))
                    holder.getView<TextView>(R.id.tvType_Notice).background = ResourceUtils.getDrawable(R.drawable.shape_frame_orange_no_raidus)
                }
                3 -> {
                    holder.setText(R.id.tvType_Notice , "投票")
                    holder.setTextColor(R.id.tvType_Notice  , ColorUtils.string2Int("#64C8BC"))
                    holder.getView<TextView>(R.id.tvType_Notice).background = ResourceUtils.getDrawable(R.drawable.shape_frame_green_no_radius)
                }
            }
            holder.setText(R.id.tvContent_Notice , item.summary)
            holder.setText(R.id.tvTime_Notice , item.start_time)
            holder.setText(R.id.tvTitle_Notice , item.title)

        }
    }
}